package com.latinia.limsp.test;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.util.Iterator;
import java.util.Map.Entry;
import javax.jms.JMSException;
import javax.xml.rpc.ServiceException;
import com.latinia.limsp.db.MsgPersistence;
import com.latinia.limsp.util.formatter.client.FormatterFactory;
import com.latinia.limsp.util.formatter.client.FormatterMessage;
import com.latinia.limsp.util.formatter.client.MsgContent;
import com.latinia.limsp.util.formatter.client.MsgTemplateParams;

public class SendPNS  extends SendMessage
{
	Destination destination;
	FormatterMessage pnsFormatter;
	XMLManager manager;
	Contract contract;
    public SendPNS(XMLManager manager, Contract contract) {
    	super(contract.getProperties());        	
    	this.manager=manager;
    	this.contract=contract;
	} 
	public void sendMessageJMS(int i,MsgPersistence msgPersistence)throws JMSException, Exception
    {
		pnsFormatter.setContent(destination.getMessage()+" JMS "+i);
        sendByJMS(pnsFormatter.toString());
        System.out.println("PNS "+i+" enviado por JMS");
        msgPersistence.insert(contract.getTest_case(), pnsFormatter.getID(),pnsFormatter.toString());
    }
    public void sendMessageWS(int i,MsgPersistence msgPersistence)throws MalformedURLException,ServiceException,RemoteException,Exception
    {               	
    	pnsFormatter.setContent(destination.getMessage()+" WS "+i);
        sendByWS(pnsFormatter.toString());
        System.out.println("PNS "+i+" enviado por JMS");
        msgPersistence.insert(contract.getTest_case(), pnsFormatter.getID(),pnsFormatter.toString());
    }
    public void createXML()
    {
    	destination=contract.getDestination();
    	pnsFormatter=FormatterFactory.newFormatterMessageOut("pns", "pns");
    	if (contract.getId()!=null) pnsFormatter.setIdContract(contract.getId());
    	else if (contract.getRef() != null) {
    		pnsFormatter.setLoginEnterprise(contract.getCompany());
    		pnsFormatter.setRefContract(contract.getRef());
        }
    	if(destination.getKey()!=null){
    		if(!destination.getKey().equals("#ref_user")){
    			pnsFormatter.setCustomerKeyName(destination.getKey());
    		}
    	}	
    	pnsFormatter.setCustomerKeyValue(destination.getValue());
    	
    	
    	if(contract.getTemplate()!=null){
    		if(!contract.getTemplate().equals("")){
    			MsgContent content=pnsFormatter.setContent("PNS");
        		content.setRefTemplate(contract.getTemplate());
//        		content.setMainContent(true);
        		if(manager.getTemplates().get(contract.getTemplate())!=null){
        			content.setRefTemplateParams(contract.getTemplate());
            		MsgTemplateParams msgTemplateParams=pnsFormatter.addTemplateParams(contract.getTemplate());
            		Iterator<Entry<String,String>> iterator=manager.getTemplates().get(contract.getTemplate()).entrySet().iterator();
            		while(iterator.hasNext()){
            			Entry<String,String> e=(Entry<String,String>)iterator.next();
                		String key=(String)e.getKey(),value=(String)e.getValue();
                		msgTemplateParams.setParamValue(key,value );
            		}
        		}        		
    		}    		
    	}	
    }
}
